/**
 * 2017年7月27日
 */
package com.alibaba.study.io;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.channels.spi.SelectorProvider;
import java.util.Iterator;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
//
//import io.netty.channel.ChannelHandler;
//import io.netty.channel.ChannelInboundHandler;
//import io.netty.channel.ChannelOutboundHandler;
//import io.netty.channel.ChannelPipeline;
//import io.netty.channel.nio.NioEventLoop;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.util.concurrent.CompleteFuture;
//import io.netty.util.concurrent.DefaultEventExecutorGroup;
//import io.netty.util.concurrent.EventExecutor;
//import io.netty.util.concurrent.EventExecutorGroup;
//import io.netty.util.concurrent.Future;
//import io.netty.util.concurrent.GenericFutureListener;
//import io.netty.util.concurrent.GlobalEventExecutor;

/**
 * @author Alex
 *
 */
public class Main {
//	protected static Logger logger  = LoggerFactory.getLogger(Main.class);
//	protected static void logException(Throwable t) {
//		logger.error("Exception occurs: {}",t.getMessage(),t);
//	}
//	public static void main(String[] args) {
//		EventExecutorGroup eeg = new DefaultEventExecutorGroup(5);
//		for(int i = 0;i<100;i++){
//			Future<String> f = eeg.submit(new Callable<String>() {
//				@Override
//				public String call() throws Exception {
//					logger.info("doing the task");
//					TimeUnit.SECONDS.sleep(5);
//					return "result";
//				}
//				
//				
//			});
//			f.addListener(new GenericFutureListener<Future<? super String>>() {
//				@Override
//				public void operationComplete(Future<? super String> future) throws Exception {
//					logger.info("in the listener ,rsult is {}",future.get());
//					
//				}
//			});
//		}
//		
//		
//		
//	}
//	
//	public static void printHandlers(ChannelPipeline cpp){
//		Iterator<Entry<String,ChannelHandler>> it = cpp.iterator();
//	
//		logger.info("in");
//		while(it.hasNext()){
//			Entry<String,ChannelHandler> entry = it.next();
//		
//			if(entry.getValue() instanceof  ChannelInboundHandler)
//				logger.info("{}    ----    {}",entry.getKey(),entry.getValue().toString());
//		}
//		
//		it = cpp.iterator();
//		logger.info("out");
//		while(it.hasNext()){
//			Entry<String,ChannelHandler> entry = it.next();
//			if(entry.getValue() instanceof  ChannelOutboundHandler)
//				logger.info("{}    ----    {}",entry.getKey(),entry.getValue().toString());
//		}
//		
////		logger.info("second:{}",cpp.first());
////		logger.info("first:{}",cpp.last());
//		
//	}
	public static void main2(String[] args) throws Exception {
		SelectorProvider p = SelectorProvider.provider();
		
		Selector sel1 = p.openSelector();
		//Selector sel2 = p.openSelector();
		
		ServerSocketChannel sc1 = ServerSocketChannel.open();
		//ServerSocketChannel sc2 = ServerSocketChannel.open();
		sc1.configureBlocking(false);
		//sc2.configureBlocking(false);
		sc1.socket().setReuseAddress(true);
		//sc2.socket().setReuseAddress(true);
		
		
		sc1.socket().bind(new InetSocketAddress("localhost",8081));
		//sc2.socket().bind(new InetSocketAddress("localhost",8082));
		
		//sc1.register(, ops)
		sc1.register(sel1, SelectionKey.OP_ACCEPT);
		//sc2.register(sel2, SelectionKey.OP_ACCEPT);
		//sc1.register(sel2, SelectionKey.OP_ACCEPT);
		Thread t1 = new Thread(()->{
			try{
			handler(sel1);}catch(Exception e){e.printStackTrace();}
		});
		t1.start();
	//	Thread t2 = new Thread(()->{
	//		try{
		//		handler(sel2);}catch(Exception e){e.printStackTrace();}
	//	});
	//	t2.start();
//		ss.setReuseAddress(true);
	
		t1.join();
	//	t2.join();
		
	}
	static void handler(Selector sel) throws IOException{
		while(true){
			sel.select();
			Iterator<SelectionKey> keyIter = sel.selectedKeys().iterator();
			while(keyIter.hasNext()){
				SelectionKey sk = keyIter.next();
				if(sk.isAcceptable()){
					System.out.println(sk);
					ServerSocketChannel sc = (ServerSocketChannel) sk.channel();
					SocketChannel scc = sc.accept();
					ByteBuffer by = ByteBuffer.wrap("hello word".getBytes());
					//logic 
					scc.write(by);
					
					scc.close();
				}
				
			}
		}
	}
}
